/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.menosehmais.controller.rest;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import static br.com.caelum.vraptor.view.Results.json;

import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.menosehmais.business.ServicoDeDesignBO;
import br.com.menosehmais.dto.ServicoDeDesignDTO;
import br.com.menosehmais.framework.loggin.StackTraceLogger;
import br.com.menosehmais.framework.persistence.DomainObject;
import br.com.menosehmais.framework.persistence.Id;
import br.com.menosehmais.framework.presentation.AbstractRestController;
import br.com.menosehmais.model.ServicoDeDesignDO;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Junior JAFJ
 */
@Resource
public class ManterServicoDesignController extends AbstractRestController {
        private final ServicoDeDesignBO servicoBO;

        @Path("/manterServicoDesign")
        public void indexManterServicoDesign() {}

        public ManterServicoDesignController(HttpServletRequest request, Result result, Validator validator,
                ServicoDeDesignBO servicoBO) {
            super(request, result, validator);
            this.servicoBO = servicoBO;
        }

        @Post
        @Path("/rest/servicoDesign/")
        public void create(ServicoDeDesignDTO servicoDTO) {
            validator.validate(servicoDTO);
            if (validator.hasErrors()) {
                validator.onErrorForwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
            }
            ServicoDeDesignDO servicoDO = servicoDTO.toDomainObject();
            DomainObject domain = servicoBO.insert(servicoDO);
            if (domain != null) {
                result.use(json()).withoutRoot().from(domain).serialize();
            } else {
                result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
            }
        }

        @Put
        @Path("/rest/servicoDesign/{id}")
        public void update(Id id, ServicoDeDesignDTO servicoDTO) {
            servicoDTO.setId(id.toString());
            validator.validate(servicoDTO);
            if (validator.hasErrors()) {
                validator.onErrorForwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
            }
            ServicoDeDesignDO servicoDO = servicoDTO.toDomainObject();
            DomainObject domain = servicoBO.update(servicoDO);
            if (domain != null) {
                result.use(json()).withoutRoot().from(domain).serialize();
            } else {
                result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
            }
        }

        @Delete
        @Path("/rest/servicoDesign/{id}")
        public void destroy(Id id, ServicoDeDesignDTO servicoDTO) {
            ServicoDeDesignDO servicoDO = servicoBO.selectById(id.toString());
            if (servicoDO != null) {
                try {
                    servicoBO.delete(servicoDO);
                    result.use(json()).withoutRoot().from(true).serialize();
                } catch (Exception e) {
                    StackTraceLogger.INSTANCE.printStackTrace(e);
                    result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
                }
            } else {
                result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
            }
        }

        @Get
        @Path("/rest/servicoDesign/{filtro}")
        public void show(String filtro, ServicoDeDesignDTO servicoDTO) {
            servicoDTO = new ServicoDeDesignDTO();
            servicoDTO.setFiltroNome(filtro);
            Collection<ServicoDeDesignDO> servicoDO = servicoBO.listarServicosDesignFiltrados(servicoDTO);
            if (servicoDO != null) {
                result.use(json()).withoutRoot().from(servicoDO).serialize();
            } else {
                result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
            }
        }

        @Post
        @Path("/rest/servicoDesign/{id}")
        public void detail(Id id) {
            ServicoDeDesignDO servicoDO = servicoBO.selectById(id.toString());
            if (servicoDO != null) {
                result.use(json()).withoutRoot().from(servicoDO).serialize();
            } else {
                result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
            }            
        }
}
